import React from "react";
import ReactDOM from 'react-dom';
import { ListView } from 'antd-mobile/lib/index';
// import Header from "../components/Header";
import {connect} from "react-redux";
import {clearData, saveFormData} from "../../store/home/action";
import PropTypes from "prop-types";

const data = [
    {
        img: 'https://zos.alipayobjects.com/rmsportal/dKbkpPXKfvZzWCM.png',
        title: 'Meet hotel',
        des: '不是所有的兼职汪都需要风吹日晒',
    },
    {
        img: 'https://zos.alipayobjects.com/rmsportal/XmwCzSeJiqpkuMB.png',
        title: 'McDonald\'s invites you',
        des: '不是所有的兼职汪都需要风吹日晒',
    },
    {
        img: 'https://zos.alipayobjects.com/rmsportal/hfVtzEhPzTUewPm.png',
        title: 'Eat the week',
        des: '不是所有的兼职汪都需要风吹日晒',
    },
];
const NUM_ROWS = 10;
let pageIndex = 0;

function genData(pIndex = 0) {
    const dataArr = [];
    for (let i = 0; i < NUM_ROWS; i++) {
        dataArr.push(`row - ${(pIndex * NUM_ROWS) + i}`);
    }

    return dataArr;
}

class Home extends React.Component{
    static propTypes = {
        formData: PropTypes.object.isRequired,
        saveFormData: PropTypes.func.isRequired,
        clearData: PropTypes.func.isRequired,
    }
    constructor(props) {
        super(props);
        console.log(this)
    }

    componentWillMount() {

        const dataSource = new ListView.DataSource({
            rowHasChanged: (row1, row2) => row1 !== row2,
        });
        if(this.props.formData.state&&this.props.formData.state.list){
            let state=this.props.formData.state;
            state.dataSource=dataSource.cloneWithRows(state.list);
            state.pageSize=state.list.length;
            console.log(state.pageSize)
            this.setState(
                state
            )
        }else{
            this.setState(
                {
                    dataSource,
                    refreshing: true,
                    isLoading: true,
                    height: document.documentElement.clientHeight,
                    useBodyScroll: true,
                    pageSize:NUM_ROWS
                }
            )
        }


    }
    //渲染后，开始加载数据
    componentDidMount() {
        if(!(this.props.formData.state&&this.props.formData.state.list)){
            const hei = this.state.height - ReactDOM.findDOMNode(this.lv).offsetTop;
            let list = genData();
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(list),
                height:hei,
                refreshing: false,
                isLoading: false,
                list:list
            });
        }else{
            // this.setState({
            //    pageSize:NUM_ROWS
            // });
            window.scrollTo(0,1);
            this.timmer=setTimeout(() => {
                window.scrollTo(0,this.props.formData.height);
            }, 1);

        }
    }

    componentWillUnmount() {
        console.log(window.scroll.getScrollTop())
        this.props.saveFormData({
            height:window.scroll.getScrollTop(),
            state:this.state
        })
        clearTimeout(this.timmer);
    }



    onEndReached = (event) => {
        if (this.state.isLoading && !this.state.hasMore) {
            return;
        }
        console.log('reach end', event);
        this.setState({ isLoading: true });
        setTimeout(() => {
            let list = [...this.state.list, ...genData(++pageIndex)];
            this.setState({
                dataSource: this.state.dataSource.cloneWithRows(list),
                list:list,
                isLoading: false,
            });
        }, 1000);
    };

    render() {

        const separator = (sectionID, rowID) => (
            <div
            
                key={`${sectionID}-${rowID}`}
                style={{
                    backgroundColor: '#F5F5F9',
                    height: 8,
                    borderTop: '1px solid #ECECED',
                    borderBottom: '1px solid #ECECED',
                }}
            />
        );
        let index = data.length - 1;
        const row = (rowData, sectionID, rowID) => {
            console.log(rowData)
            if (index < 0) {
                index = data.length - 1;
            }
            const obj = data[index--];
            return (
                <div key={rowID}
                     style={{
                         padding: '0 15px',
                         backgroundColor: 'white',
                     }}
                >
                    <div style={{ height: '50px', lineHeight: '50px', color: '#888', fontSize: '18px', borderBottom: '1px solid #ddd' }}>
                        {obj.title}
                    </div>
                    <div className={"flex"}>
                        <img style={{ height: '63px', width: '63px', marginRight: '15px' }} src={obj.img} alt="" />
                        <div style={{ display: 'inline-block' }}>
                            <div style={{ marginBottom: '8px', color: '#000', fontSize: '16px', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', maxWidth: '250px' }}>{obj.des}-{rowData}</div>
                            <div style={{ fontSize: '16px' }}><span style={{ fontSize: '30px', color: '#FF6E27' }}>{rowID}</span> 元/任务</div>
                        </div>
                    </div>
                </div>
            );
        };
        return (<div>
            <ListView
                key={"listIndex"}
                ref={el => this.lv = el}
                dataSource={this.state.dataSource}
                // renderHeader={() => <span>Pull to refresh</span>}
                renderFooter={() => (<div style={{ padding: 30, textAlign: 'center' }}>
                    {this.state.isLoading ? 'Loading...' : 'Loaded'}
                </div>)}
                renderRow={row}
                renderSeparator={separator}
                useBodyScroll={this.state.useBodyScroll}
                onEndReached={this.onEndReached}
                pageSize={this.state.pageSize}
            />
        </div>);
    }
}

export default connect(store => ({
    formData:store.homeData
}),{saveFormData, clearData})(Home);


